package com.opmsys.mapper;

import com.opmsys.pojo.entity.Well;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface WellMapper {
    /**
     * 新增油井
     * @param well
     */
    @Insert("INSERT INTO t_well ( well_name, well_type, well_depth, well_diameter, well_date, well_x, well_y) VALUES ( #{well.wellName}, #{well.wellType}, #{well.wellDepth}, #{well.wellDiameter}, #{well.wellDate}, #{well.wellX}, #{well.wellY})")
    void insertWell(@Param("well") Well well);

    /**
     * 删除油井
     * @param id
     */
    @Delete("DELETE FROM t_well WHERE id = #{id}")
    void deleteWell(@Param("id") Integer id);

    /**
     * 更新油井信息
     * @param well
     */
    @Update("<script>" +
            "UPDATE t_well " +
            "<set>" +
            "<if test='well.wellName != null and well.wellName != \"\" '> well_name = #{well.wellName}, </if>" +
            "<if test='well.wellType != null'> well_type = #{well.wellType}, </if>" +
            "<if test='well.wellDepth != null'> well_depth = #{well.wellDepth}, </if>" +
            "<if test='well.wellDiameter != null'> well_diameter = #{well.wellDiameter}, </if>" +
            "<if test='well.wellDate != null and well.wellDate != \"\" '> well_date = #{well.wellDate}, </if>" +
            "<if test='well.wellX != null'> well_x = #{well.wellX}, </if>" +
            "<if test='well.wellY != null'> well_y = #{well.wellY}, </if>" +
            "</set> " +
            "WHERE id = #{well.id}" +
            "</script>")
    void updateWell(@Param("well") Well well);

    /**
     * 模糊查询井名与精准查询井类型
     * @param wellType
     * @param wellName
     * @return
     */
    @Select("<script>" +
            "SELECT * FROM t_well " +
            "<where>" +
            "<if test='wellType != null'> AND well_type = #{wellType} </if>" +
            "<if test='wellName != null'> AND well_name LIKE CONCAT('%', #{wellName}, '%') </if>" +
            "</where> " +
            "ORDER BY id " +
            "</script>")
    List<Well> searchWells(@Param("wellType") Integer wellType, @Param("wellName") String wellName);

    /**
     * 模糊查询井名与精准查询井类型并分页
     * @param wellType
     * @param wellName
     * @param pageSize
     * @param offset
     * @return
     */
    @Select("<script>" +
            "SELECT * FROM t_well " +
            "<where>" +
            "<if test='wellType != null'> AND well_type = #{wellType} </if>" +
            "<if test='wellName != null'> AND well_name LIKE CONCAT('%', #{wellName}, '%') </if>" +
            "</where> " +
            "ORDER BY id " +
            "LIMIT #{pageSize} OFFSET #{offset}"+
            "</script>")
    List<Well> listWells(@Param("wellType") Integer wellType, @Param("wellName") String wellName, @Param("pageSize") Integer pageSize, @Param("offset") Integer offset);

    /**
     * 根据井名查询
     * @param name
     * @return
     */
    @Select("SELECT * FROM t_well WHERE well_name = #{name}")
    List<Well> ifname(@Param("name") String name);
}
